package com.ruoyi.web.controller.fileblueprint;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.domain.PlanToLog;
import com.ruoyi.domain.SiteFeeSchedule;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProFileBlueprint;
import com.ruoyi.system.service.IProFileBlueprintService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 工程文件上传Controller
 *
 * @author ruoyi
 * @date 2020-06-19
 */
@Controller
@RequestMapping("/system/blueprint")
public class ProFileBlueprintController extends BaseController {
    private String prefix = "system/blueprint";

    @Autowired
    private IProFileBlueprintService proFileBlueprintService;

    @RequiresPermissions("system:blueprint:view")
    @GetMapping()
    public String blueprint() {
        return prefix + "/blueprint";
    }

    /**
     * 详情页面传递参数
     * @param bId
     * @param map
     * @return
     */
    @RequiresPermissions("ruoyi:blueprint:view")
    @GetMapping("/blueprints/{bId}")
    public String logs(@PathVariable("bId") int bId, ModelMap map) {
        ProFileBlueprint proFileBlueprint = proFileBlueprintService.selectProFileBlueprintById(bId);
        map.put("proFileBlueprint", proFileBlueprint);
        return prefix + "/blueprints";
    }

    /**
     * 查询工程文件上传列表
     */
    @RequiresPermissions("system:blueprint:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProFileBlueprint proFileBlueprint) {
        startPage();
        List<ProFileBlueprint> list = proFileBlueprintService.selectProFileBlueprintList(proFileBlueprint);
        return getDataTable(list);
    }

    /**
     * 查询对应项目下的对应文件上传
     */
    @RequiresPermissions("system:blueprint:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long filePro) {
        startPage();
        List<ProFileBlueprint> list = proFileBlueprintService.selectProFileBlueprintByFile(filePro);
        return getDataTable(list);
    }

    /**
     * 首页下对应的工程文件
     */
    @RequiresPermissions("ruoyi:log:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long idss) {
        startPage();
        List<ProFileBlueprint> list = proFileBlueprintService.selectProFileBlueprintByFile(idss);
        return getDataTable(list);
    }

    /**
     * 导出工程文件上传列表
     */
    @RequiresPermissions("system:blueprint:export")
    @Log(title = "工程文件上传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProFileBlueprint proFileBlueprint) {
        List<ProFileBlueprint> list = proFileBlueprintService.selectProFileBlueprintList(proFileBlueprint);
        ExcelUtil<ProFileBlueprint> util = new ExcelUtil<ProFileBlueprint>(ProFileBlueprint.class);
        return util.exportExcel(list, "工程文件上传");
    }

    /**
     * 新增文件上传
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存工程文件上传
     */
    @RequiresPermissions("system:blueprint:add")
    @Log(title = "工程文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, ProFileBlueprint proFileBlueprint) throws IOException {
        //上传文件路径
        String filePach = Global.getUploadPath();
        //上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePach, file);
        proFileBlueprint.setpFileUploading(fileName);
        //自动获取上传人姓名
        proFileBlueprint.setpTheHeir(ShiroUtils.getLoginName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        return toAjax(proFileBlueprintService.insertProFileBlueprint(proFileBlueprint));
    }

    /**
     * 修改工程文件上传
     */
    @GetMapping("/edit/{pFileId}")
    public String edit(@PathVariable("pFileId") Integer pFileId, ModelMap mmap) {
        ProFileBlueprint proFileBlueprint = proFileBlueprintService.selectProFileBlueprintById(pFileId);
        mmap.put("proFileBlueprint", proFileBlueprint);
        return prefix + "/edit";
    }

    /**
     * 修改保存工程文件上传
     */
    @RequiresPermissions("system:blueprint:edit")
    @Log(title = "工程文件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProFileBlueprint proFileBlueprint) {
        return toAjax(proFileBlueprintService.updateProFileBlueprint(proFileBlueprint));
    }

    /**
     * 删除工程文件上传
     */
    @RequiresPermissions("system:blueprint:remove")
    @Log(title = "工程文件上传", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(proFileBlueprintService.deleteProFileBlueprintByIds(ids));
    }

    /**
     * 下载
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("pFileUploading") String pFileUploading, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(pFileUploading, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadNames = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;pFileName=" + FileUtils.setFileDownloadHeader(request, downloadNames));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }

    /**
     * 导出付款申请单合同费用
     */
    @Log(title = "工程文件上传", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:blueprint:exports")
    @PostMapping("/exports")
    @ResponseBody
    public AjaxResult exports(ProFileBlueprint proFileBlueprint)
    {
        List<ProFileBlueprint> list = proFileBlueprintService.selectUserexports(proFileBlueprint);
        ExcelUtil<ProFileBlueprint> util = new ExcelUtil<ProFileBlueprint>(ProFileBlueprint.class);
        return util.exportExcel(list, "工程文件上传");
    }

}
